<extend name="_Layout/main"/>


<block name="main">
    <div class="container">
        <div class="row">

            <div class="progress">
                <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
                    0%
                </div>
            </div>
            <a id="Startcontg" class="btn btn-primary" href="javascript:;">开始采集入库</a>

            <div class="table-responsive">
                <table class="table">
                    <thead>
                    <tr>
                        <th>标题&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(共计 <php>echo( $result[count] );</php>条)</th>
                        <th>URL</th>
                    </tr>
                    </thead>
                    <tbody>
                        <volist name="list" id="data">
                            <tr>
                                <td>{$data.title}</td>
                                <td>{$data.url}</td>
                            </tr>
                        </volist>
                    </tbody>
                </table>
            </div>

        </div>
    </div>
</block>

<block name="script">
    <script type="text/javascript">
      $(function(){
        var ajax_url = "<?php echo stripslashes(U('Admin/PublicQuerylist/Startcontg'));?>"; //请求地址
        var aurl = "<?php echo($result[aurl]);?>";        //域名地址
        var cid = "<?php echo($result[cid]);?>";        //父级ID
        var list =eval('<?php echo json_encode($list);?>');      //输出采集列表的数组
        var bar = $('.progress-bar');      //进度条
        var barnum = 0;     //进度条参数
        //console.log(barnum);
        var listnum = 0;        //下标
        $('#Startcontg').click(function(){
          startcontg()    //发起ajax
        });

        function startcontg(){
          $.ajax({
            type: 'GET',
            data: {cid:cid, title:list[listnum].title , url:aurl + list[listnum].url},
            url: ajax_url,
            cache: false,
            async : true,
            success: function (data ,textStatus, jqXHR) {
              if(data === '200'){
                if(list.length-1 > listnum){      //数组长度是从1算起，下标从0算起，所以数组长度需要-1才是正确匹配的;
                  listnum++;  //下标+1
                  barnum = barnum + Math.round(100 / list.length);  //进度条加自己
                  bar.attr({'aria-valuenow':barnum});   //修改进度条
                  bar.css({'width': barnum+'%'});   //修改进度条
                  bar.html(barnum + '%');   //修改进度条
                  startcontg();  //递归
                  //console.log('下标：'+listnum);
                }else{
                  bar.attr({'aria-valuenow':'100'});   //修改进度条100
                  bar.css({'width': '100%'});   //修改进度条100
                  bar.html('100%');   //修改进度条100
                  layer.msg('采集完成！');
                }
              }else{
                layer.msg('采集错误，请检测代码！');
              }
              //console.log(data);

            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
              alert("请求失败！");
            }
          });
        };
      });
    </script>
</block>

